package com.lp.chapter001.exception;

import com.lp.chapter001.util.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 * @author LP
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResultData<Object> handlerException(Exception e) {
        log.error("业务异常:{} ", e.getMessage(), e);
        // 是不是自定义异常
        if (e instanceof BizException bizException) {
            return ResultData.fail(bizException.getCode(), bizException.getMsg());
        } else {
            // 业务异常
            return ResultData.fail("系统繁忙，请稍后重试");
        }
    }
}
